package com.jiangxiaofei.citycloud.mvp;

import android.content.Context;

import com.jiangxiaofei.citycloud.bean.VillageDetailBean;
import com.jiangxiaofei.citycloud.bean.VillageMenuBean;
import com.jiangxiaofei.citycloud.bean.VillageNewsBean;
import com.jiangxiaofei.citycloud.httpUtil.HttpResult;
import com.jiangxiaofei.citycloud.httpUtil.OnLoadDataIm;
import com.jiangxiaofei.citycloud.httpUtil.RetrofitUtils;
import com.jiangxiaofei.citycloud.util.LogUtil;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by 上官林超 on 2017/7/30.
 */

public class VillageModel implements VillageConstant.VillageMenuModel, VillageConstant.VillageListModel
        , VillageConstant.VillageDetailModel, VillageConstant.VillageAddArticleModel, VillageConstant.VillageUpdateArticleModel {

    //获取美丽乡村菜单
    @Override
    public void getVillageMenu(Context mContext, final OnLoadDataIm callBack) {
        RetrofitUtils.getApiService(mContext).getVillageMenu()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<VillageMenuBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.e(e.toString());
                    }

                    @Override
                    public void onNext(HttpResult<List<VillageMenuBean>> listHttpResult) {
                        callBack.onSuccess(listHttpResult.getData(), listHttpResult.getCode());
                    }
                });
    }

    //获取文章列表
    @Override
    public void getVillageList(Context mContext, String page, String category, final OnLoadDataIm callBack) {

        RetrofitUtils.getApiService(mContext)
                .getVillageList(page, category)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<VillageNewsBean>>>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        callBack.onFailure("没有更多数据了",0);
                        LogUtil.e(e.toString());
                    }

                    @Override
                    public void onNext(HttpResult<List<VillageNewsBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callBack.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callBack.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });

    }

    //获取文章详情
    @Override
    public void getVillageDetail(Context mContext, int id, final OnLoadDataIm callBack) {
        RetrofitUtils.getApiService(mContext)
                .getVillageDetail(id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<VillageDetailBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.i(e.toString());
                    }

                    @Override
                    public void onNext(HttpResult<List<VillageDetailBean>> listHttpResult) {
                        if (listHttpResult.getData() != null) {
                            callBack.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    //添加文章
    @Override
    public void addVillageArticle(Context mContext, String title, String content, int author, int category, String img, String location, final OnLoadDataIm callBack) {
        RetrofitUtils.getApiService(mContext)
                .addAddarticle(title, content, author, category, img, location)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.e(e.toString());
                    }

                    @Override
                    public void onNext(HttpResult<String> stringHttpResult) {
                        callBack.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                    }
                });
    }

    //修改文章
    @Override
    public void updateVillageArticle(Context mContext, String title, String content, int author, int category, String img, String location, int id, final OnLoadDataIm callBack) {
        RetrofitUtils.getApiService(mContext)
                .updateAddarticle(title, content, author, category, img, location, id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<String>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        LogUtil.e(e.toString());
                    }

                    @Override
                    public void onNext(HttpResult<String> stringHttpResult) {
                        callBack.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                    }
                });
    }
}
